package com.boot.springboot.component.qlexpress;

import com.boot.springboot.component.qlexpress.domain.UserInfo;
import com.boot.springboot.component.qlexpress.service.QlService;
import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import com.ql.util.express.IExpressContext;
import org.junit.Test;

import java.util.HashMap;

/**
 * @author luowenjie
 * @since 2022/2/12 15:47
 */
public class MainTest {

    public static void main(String[] args) throws Exception {
        /*ExpressRunner runner = new ExpressRunner();
        DefaultContext<String, Object> context = new DefaultContext<String, Object>();
        context.put("a", 1);
        context.put("b", 2);
        context.put("c", 3);
        String express = "a + b * c";
        Object r = runner.execute(express, context, null, true, false);
        System.out.println(r);*/
        QlService demo = new QlService();
        demo.initial();
        System.out.println(demo.hasPermission(new UserInfo(100,"xuannan",3),  "三星卖家   而且   已经开店"));
        System.out.println(demo.hasPermission(new UserInfo(101,"qianghui",3), "三星卖家   或者   已经开店"));
        System.out.println(demo.hasPermission(new UserInfo(101,"张三",3), "三星卖家 and 已经开店"));
        System.out.println(demo.hasPermission(new UserInfo(100,"李四",3), "三星卖家 and 已经开店"));
        ExpressRunner runner = new ExpressRunner();
        runner.addMacro("计算平均成绩", "(语文+数学+英语)/3.0");
        runner.addMacro("是否优秀", "计算平均成绩>90");
        IExpressContext<String, Object> context = new DefaultContext<String, Object>();
        context.put("语文", 88);
        context.put("数学", 99);
        context.put("英语", 95);
        Object result = runner.execute("是否优秀", context, null, false, false);
        System.out.println(result);

        String express = "int 平均分 = (语文 + 数学 + 英语 + 综合考试.科目2) / 4.0; return 平均分";
        //ExpressRunner runner = new ExpressRunner(true, true);
        String[] names = runner.getOutVarNames(express);
        for(String s:names){
            System.out.println("var : " + s);
        }

    }

    @Test
    public void testSet() throws Exception {
        ExpressRunner runner = new ExpressRunner(false, false);
        DefaultContext<String, Object> context = new DefaultContext<String, Object>();
        String express = "abc = NewMap(1:1, 2:2); return abc.get(1) + abc.get(2);";
        Object r = runner.execute(express, context, null, false, false);
        System.out.println(r);
        express = "abc = NewList(1, 2, 3); return abc.get(1) + abc.get(2)";
        r = runner.execute(express, context, null, false, false);
        System.out.println(r);
        express = "abc = [1, 2, 3]; return abc[1] + abc[2];";
        r = runner.execute(express, context, null, false, false);
        System.out.println(r);
    }

    /**
     * 三目运算符测试
     * @throws Exception if any
     */
    @Test
    public void logicalTernaryOperationsTest() throws Exception {
        final String express =
                "a = 1;b = 2;maxnum = a > b ? a : b;";
        ExpressRunner runner = new ExpressRunner();
        DefaultContext<String, Object> context = new DefaultContext<>();
        Object r = runner.execute(express, context, null, true, false);
        System.out.println(r);
    }

    @Test
    public   void collectionStatement() throws Exception {
        ExpressRunner runner = new ExpressRunner();
        DefaultContext<String, Object> defaultContext = new DefaultContext<>();
        HashMap<String, String> mapData = new HashMap() {{
            put("a", "hello");
            put("b", "world");
            put("c", "!@#$");
        }};
        defaultContext.put("map", mapData);
        //ql不支持for(obj:list){}的语法，只能通过下标访问。
        String mapTraverseStatement = " keySet = map.keySet();\n" +
                "  objArr = keySet.toArray();\n" +
                "  for (i=0;i<objArr.length;i++) {\n" +
                "  key = objArr[i];\n" +
                "   System.out.println(map.get(key));\n" +
                "  }";
        runner.execute(mapTraverseStatement, defaultContext, null, false, false);
        System.out.println(defaultContext);
    }

    @Test
    public   void collectionStatement1() throws Exception {
        ExpressRunner runner = new ExpressRunner();
        DefaultContext<String, Object> defaultContext = new DefaultContext<>();
        HashMap<String, String> mapData = new HashMap() {{
            put("a", "hello");
            put("b", "world");
            put("c", "!@#$");
        }};
        defaultContext.put("map", mapData);
        //ql不支持for(obj:list){}的语法，只能通过下标访问。
        String mapTraverseStatement = " keySet = map.keySet();\n" +
                "  objArr = keySet.toArray();\n" +
                "  for (i=0;i<objArr.length;i++) {\n" +
                "  key = objArr[i];\n" +
                "   System.out.println(map.get(key));\n" +
                "  }" +
                "map";
        Object result= runner.execute(mapTraverseStatement, defaultContext, null, false, false);
        System.out.println(defaultContext);
    }

}
